package jgl.vajra.driver.widget.manager;

import jgl.vajra.driver.widget.dialogfragment.LoadingDialogFragment;
import jgl.vajra.driver.widget.interfaces.DialogFragmentKeyListener;
import jgl.vajra.driver.widget.interfaces.DialogFragmentStatesListener;
import jgl.vajra.driver.widget.mvvm.view.AppActivityManager;

import androidx.fragment.app.FragmentActivity;

/**
 * @Author zhangshuqi
 * @CreateTime 2018/4/11
 * @Describe
 */

public class LoadingFragmentManager {
    private LoadingDialogFragment loadingDialogFragment;

    private LoadingFragmentManager() {

    }


    private static class LoadingFragmentManagerSingleton {
        private static final LoadingFragmentManager INSTANCE = new LoadingFragmentManager();

    }

    public static LoadingFragmentManager getInstance() {
        return LoadingFragmentManager.LoadingFragmentManagerSingleton.INSTANCE;
    }

    public void showLoadingDialog(FragmentActivity activity, String loadingText) {
        if (activity == null)
            activity = AppActivityManager.getAppActivityManager().currentActivity();
        if (activity == null)
            return;
        if (loadingDialogFragment != null && loadingDialogFragment.isShowing()) {
            loadingDialogFragment.dismiss();
        }
        if (loadingDialogFragment != null)
            loadingDialogFragment = null;
        loadingDialogFragment = new LoadingDialogFragment();
        loadingDialogFragment.setLoadingText(loadingText);
        loadingDialogFragment.show(activity.getSupportFragmentManager());
    }

    public void showLoadingDialog(String loadingText) {
        showLoadingDialog(AppActivityManager.getAppActivityManager().currentActivity(), loadingText);
    }

    public void dismissLoadingDialog() {

        if (loadingDialogFragment == null) return;
        if (loadingDialogFragment.isShowing()) {
            loadingDialogFragment.dismiss();
            loadingDialogFragment = null;
        }
    }

    public void setUseBlur(boolean isUseBulr) {
        if (loadingDialogFragment == null) return;
        loadingDialogFragment.setUseBlur(isUseBulr);
    }

    public void setDialogFragmentStatesListener(DialogFragmentStatesListener dialogFragmentStatesListener) {
        if (loadingDialogFragment == null) return;
        loadingDialogFragment.setDialogFragmentStatesListener(dialogFragmentStatesListener);
    }

    public void setDialogFragmentKeyListener(DialogFragmentKeyListener dialogInterface) {
        if (loadingDialogFragment == null) return;
        loadingDialogFragment.setDialogFragmentKeyListener(dialogInterface);
    }

}

